﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class TrangChu : System.Web.UI.Page
{

   
    #region Private Properties
    private int CurrentPage
    {
        get
        {
            object objPage = ViewState["_CurrentPage"];
            int _CurrentPage = 0;
            if (objPage == null)
            {
                _CurrentPage = 0;
            }
            else
            {
                _CurrentPage = (int)objPage;
            }
            return _CurrentPage;
        }
        set { ViewState["_CurrentPage"] = value; }
    }
    private int fistIndex
    {
        get
        {

            int _FirstIndex = 0;
            if (ViewState["_FirstIndex"] == null)
            {
                _FirstIndex = 0;
            }
            else
            {
                _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]);
            }
            return _FirstIndex;
        }
        set { ViewState["_FirstIndex"] = value; }
    }
    private int lastIndex
    {
        get
        {

            int _LastIndex = 0;
            if (ViewState["_LastIndex"] == null)
            {
                _LastIndex = 0;
            }
            else
            {
                _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]);
            }
            return _LastIndex;
        }
        set { ViewState["_LastIndex"] = value; }
    }
    #endregion

    #region PagedDataSource
    PagedDataSource _PageDataSource = new PagedDataSource();
    #endregion
    SoLienLacDataContext db = new SoLienLacDataContext();
    clsThaoTac cls = new clsThaoTac();
    private void BindItemsList()
    {
        DataTable dt = this.LoadTinTucHocTap();
        _PageDataSource.DataSource = dt.DefaultView;
        _PageDataSource.AllowPaging = true;
        _PageDataSource.PageSize = 10;
        _PageDataSource.CurrentPageIndex = CurrentPage;
        ViewState["TotalPages"] = _PageDataSource.PageCount;

        this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount;
        this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnNext.Enabled = !_PageDataSource.IsLastPage;
        this.lbtnFirst.Enabled = !_PageDataSource.IsFirstPage;
        this.lbtnLast.Enabled = !_PageDataSource.IsLastPage;

        this.dtlGD.DataSource = _PageDataSource;
        this.dtlGD.DataBind();
        this.doPaging();
    }
    private void doPaging()
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("PageIndex");
        dt.Columns.Add("PageText");

        fistIndex = CurrentPage - 5;


        if (CurrentPage > 5)
        {
            lastIndex = CurrentPage + 5;
        }
        else
        {
            lastIndex = 10;
        }
        if (lastIndex > Convert.ToInt32(ViewState["TotalPages"]))
        {
            lastIndex = Convert.ToInt32(ViewState["TotalPages"]);
            fistIndex = lastIndex - 10;
        }

        if (fistIndex < 0)
        {
            fistIndex = 0;
        }

        for (int i = fistIndex; i < lastIndex; i++)
        {
            DataRow dr = dt.NewRow();
            dr[0] = i;
            dr[1] = i + 1;
            dt.Rows.Add(dr);
        }

        this.dlPaging.DataSource = dt;
        this.dlPaging.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            LoadTinMoi();
            this.BindItemsList();
            //  LoadTinTucHocTap();
        }
    }

    DataTable LoadTinTucHocTap()
    {
        var c = from p in db.TinTucs where p.MaChuyenMuc == int.Parse("1") select new { p.TieuDe, p.TomTatNoiDung, p.MaTin, p.MaChuyenMuc, p.Anh };
        DataTable dt = new DataTable();
        dt.Columns.Add("Anh", typeof(string));
        dt.Columns.Add("MaTin", typeof(string));
        dt.Columns.Add("MaChuyenMuc", typeof(string));
        dt.Columns.Add("TieuDe", typeof(string));
        dt.Columns.Add("TomTatNoiDung", typeof(string));
        foreach (var c1 in c)
        {
            DataRow dr = dt.NewRow();
            dr[0] = c1.Anh.ToString();
            dr[1] = c1.MaTin.ToString();
            dr[2] = c1.MaChuyenMuc.ToString();
            dr[3] = c1.TieuDe.ToString();
            dr[4] = c1.TomTatNoiDung.ToString();
            dt.Rows.Add(dr);
        }
        dtlGD.DataSource = dt;
        dtlGD.DataBind();
        return dt;
    }
    void LoadTinMoi()
    {
        dtlMoi.DataSource = db.TinMoi();
        dtlMoi.DataBind();
    }
    
    
    protected void btnDangNhap_Click(object sender, EventArgs e)
    {
        bool kt = true;
        txtPass.Attributes.Add("value", txtPass.Text);
        kt = KTCheck();
        if (kt == false)
        { return; }
        kt = KT();
        if (kt == false)
        { return; }
        if (rdoAdmin.Checked == true)
        {
            var c = from p in db.Admins select p;
            foreach (var c1 in c)
            {
                if (txtDangNhap.Text == c1.TenDangNhap.Trim() && txtPass.Text == c1.MatKhau.Trim())
                {
                    Session["Username"] = txtDangNhap.Text.ToString();
                    Session["TrangThai"] = "DaDangNhap";
                    Session["TenAdmin"] = c1.MaAdmin;
                    string url = Request.QueryString["url"];
                    if (!string.IsNullOrEmpty(url))
                        Response.Redirect(url);
                    else
                        Response.Redirect("~/QuanTri/Default.aspx");
                }
                if (txtDangNhap.Text == c1.TenDangNhap.Trim() || txtPass.Text == c1.MatKhau.Trim())
                {
                    lblThongBao.Text = "Tên đăng nhập hoặc mật khẩu chưa đúng";
                    txtDangNhap.Text = "";
                    txtPass.Text = "";
                    txtDangNhap.Focus();
                }
            }
        }
        if (rdoGiaoVien.Checked == true)
        {
            var c = from p in db.CanBoGiaoViens select new { p.MatKhau, p.MaCanBoGV, p.TenCanBo };
            foreach (var c1 in c)
            {
                if (txtDangNhap.Text == c1.MaCanBoGV && txtPass.Text == c1.MatKhau.Trim())
                {
                    Session["Username"] = txtDangNhap.Text.ToString();
                    Session["TrangThai"] = "DaDangNhap";
                    Session["TenCanBo"] = c1.TenCanBo;
                    string url = Request.QueryString["url"];
                    if (!string.IsNullOrEmpty(url))
                        Response.Redirect(url);
                    else
                        Response.Redirect("~/giaovien/DefaultGV.aspx");
                }
                if (txtDangNhap.Text == c1.MaCanBoGV.Trim() || txtPass.Text == c1.MatKhau.Trim())
                {
                    lblThongBao.Text = "Mã giáo viên hoặc mật khẩu chưa đúng";
                    txtDangNhap.Text = "";
                    txtPass.Text = "";
                    txtDangNhap.Focus();
                }

            }
        }
        if (rdoPhuHuynh.Checked == true)
        {
            var c = from p in db.HocSinhs select new { p.MaHS, p.TenHS,p.MatKhau };
            foreach (var c1 in c)
            {
                if (txtDangNhap.Text == c1.MaHS.Trim().ToString()&&txtPass.Text==c1.MatKhau.Trim())
                {
                    Session["Username"] = txtDangNhap.Text.ToString();
                    Session["TrangThai"] = "DaDangNhap";
                    Session["TenHS"] = c1.TenHS;
                    string url = Request.QueryString["url"];
                    if (!string.IsNullOrEmpty(url))
                        Response.Redirect(url);
                    else
                        Response.Redirect("~/GiaoDienClient/TrangThongTinChiTietDiem.aspx?MaHS=" + this.txtDangNhap.Text);
                }
                if (txtDangNhap.Text != c1.MaHS.Trim().ToString())
                {

                    lblThongBao.Text = "Mã học sinh chưa đúng";
                    txtDangNhap.Text = "";
                    txtPass.Text = "";
                    txtDangNhap.Focus();
                }

            }
        }
    }
    bool KTCheck()
    {
        bool kt = true;
        if (rdoAdmin.Checked == false && rdoGiaoVien.Checked == false && rdoPhuHuynh.Checked == false)
        {
            kt = false;
        }
        return kt;
    }
   
    bool KT()
    {
        bool kt = true;
        if (rdoGiaoVien.Checked == true || rdoAdmin.Checked == true)
        {
            if (txtPass.Text == "" || txtDangNhap.Text == "")
            {
                kt = false;
                lblThongBao.Text = "Bạn chưa nhập đầy đủ thông tin";
            }
        }
        if (rdoPhuHuynh.Checked == true)
        {
            if (txtDangNhap.Text == "")
            {
                kt = false;
                lblThongBao.Text = "Bạn chưa nhập đầy đủ thông tin";
            }
        }
        return kt;
    }
    protected void rdoAdmin_CheckedChanged(object sender, EventArgs e)
    {
        bool kt = false;
        if (rdoAdmin.Checked == true)
        {
            kt = true;
            rdoGiaoVien.Checked = false;
            rdoPhuHuynh.Checked = false;
            lblTenDangNhap.Text = "Tài khoản";
            lblMatKhau.Visible = true;
            txtPass.Visible = true;
            return;
        }
    }
    protected void rdoGiaoVien_CheckedChanged(object sender, EventArgs e)
    {
        bool kt = false;
        if (rdoGiaoVien.Checked == true)
        {
            kt = true;
            rdoAdmin.Checked = false;
            rdoPhuHuynh.Checked = false;
            lblTenDangNhap.Text = "Mã GV";
            lblMatKhau.Visible = true;
            txtPass.Visible = true;
        }
    }
    protected void rdoPhuHuynh_CheckedChanged(object sender, EventArgs e)
    {
        bool kt = false;
        if (rdoPhuHuynh.Checked == true)
        {
            kt = KT();
            if (kt == false)
            { return; }
            rdoGiaoVien.Checked = false;
            rdoAdmin.Checked = false;
            lblTenDangNhap.Text = "Mã HS";
            //lblMatKhau.Visible = false;
            //txtPass.Visible = false;
            return;
        }
    }
    protected void dtlGD_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 0;
        this.BindItemsList();
    }
    protected void lbtnPrevious_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        this.BindItemsList();
    }
    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        this.BindItemsList();
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1);
        this.BindItemsList();
    }
    protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e)
    {

        if (e.CommandName.Equals("Paging"))
        {
            CurrentPage = Convert.ToInt16(e.CommandArgument.ToString());
            this.BindItemsList();
        }
    }
    protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e)
    {

        LinkButton lnkbtnPage = (LinkButton)e.Item.FindControl("lnkbtnPaging");
        if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString())
        {
            lnkbtnPage.Enabled = false;
            lnkbtnPage.Style.Add("fone-size", "14px");
            lnkbtnPage.Font.Bold = true;

        }
    }
}
